package com.ydj.app.init;

import com.ydj.AppEnvironment;
import com.ydj.framework.module.init.BaseInit;
import com.ydj.app.CmsConst;
import com.ydj.app.entity.CmsAccount;
import com.ydj.app.entity.dao.CmsAccountDao;
import com.ydj.app.entity.query.CmsAccountQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class CmsAccountInit extends BaseInit {
    @Autowired
    AppEnvironment appEnvironment;

    @Autowired
    CmsAccountDao cmsAccountDao;

    @Override
    protected InitContext getContext() {
        return new InitContext("CMS-创建初始化账号", 1);
    }

    @Override
    protected void doAction() {
        String ADMIN_USER = CmsConst.DEFAULT_ADMIN_USER_NAME;

        CmsAccount account = new CmsAccountQuery(cmsAccountDao)
                .username(ADMIN_USER)
                .single();
        if (account == null) {
            account = new CmsAccount()
                    .setUsername(ADMIN_USER)
                    .setPassword(CmsConst.DEFAULT_ADMIN_PASSWORD);
            cmsAccountDao.saveOrUpdate(account);
        }

    }
}